package com.cdpm.web.controller.common;

import com.alibaba.fastjson2.JSONArray;
import com.cdpm.common.core.controller.BaseController;
import com.cdpm.common.core.domain.model.LoginUser;
import com.cdpm.common.core.page.TableDataInfo;
import com.cdpm.common.utils.CollectionUtil;
import com.cdpm.common.utils.DateUtils;
import com.cdpm.common.utils.SecurityUtils;
import com.cdpm.common.utils.StringUtils;
import com.cdpm.common.utils.bean.BeanUtils;
import com.cdpm.system.domain.StrategyBuryingPoint;
import com.cdpm.system.domain.StrategyInstance;
import com.cdpm.system.domain.StrategyInstanceNode;
import com.cdpm.system.domain.TStrategyConfig;
import com.cdpm.system.service.IStrategyBuryingPointService;
import com.cdpm.system.service.IStrategyInstanceNodeService;
import com.cdpm.system.service.IStrategyInstanceService;
import com.cdpm.system.service.IStrategyService;
import com.cdpm.web.dto.*;
import com.cdpm.web.dto.req.*;
import com.cdpm.web.dto.res.*;
import com.cdpm.web.service.StrategyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description:策略配置请求类
 * @author: longjy
 * @create: 2024-08-20 10:27
 **/
@RestController
@RequestMapping("/strategy")
public class StrategyController extends BaseController {
    @Resource
    IStrategyService iStrategyService;

    @Resource
    IStrategyInstanceService iStrategyInstanceService;

    @Resource
    IStrategyInstanceNodeService iStrategyInstanceNodeService;

    @Resource
    StrategyInfoService strategyInfoService;

    @Resource
    IStrategyBuryingPointService iStrategyBuryingPointService;

    /**
     * 策略配置列表查询
     */
    @PostMapping("/queryStrategyList")
    public QueryStrategyListResDTO queryStrategyList(@RequestBody QueryStrategyListReqDTO queryStrategyListReqDTO){
        QueryStrategyListResDTO queryStrategyListResDTO = new QueryStrategyListResDTO();
        startPage();
        List<StrategyDTO> list = new ArrayList<>();
        TStrategyConfig tStrategyConfig = new TStrategyConfig();
        BeanUtils.copyBeanProp(tStrategyConfig,queryStrategyListReqDTO);
        List<TStrategyConfig> tStrategyConfigs = iStrategyService.findList(tStrategyConfig);
        TableDataInfo dataTable = getDataTable(tStrategyConfigs);
        queryStrategyListResDTO.setRows(dataTable.getRows());
        queryStrategyListResDTO.setTotal(dataTable.getTotal());
        queryStrategyListResDTO.setMsg(dataTable.getMsg());
        queryStrategyListResDTO.setCode(dataTable.getCode());
        if(CollectionUtil.isNotEmpty(queryStrategyListResDTO.getRows())){
            List<TStrategyConfig> tStrategyConfigList = (List<TStrategyConfig>)queryStrategyListResDTO.getRows();
            for (TStrategyConfig strategyConfig : tStrategyConfigList) {
                StrategyDTO strategyDTO = new StrategyDTO();
                BeanUtils.copyBeanProp(strategyDTO,strategyConfig);
                strategyDTO.setExecuteStartDateStr(DateUtils.parseDateToStr(strategyDTO.getExecuteStartDate(),"yyyy-MM-dd HH:mm:ss"));
                strategyDTO.setExecuteEndDateStr(DateUtils.parseDateToStr(strategyDTO.getExecuteEndDate(),"yyyy-MM-dd HH:mm:ss"));
                strategyDTO.setInsertTimeStr(DateUtils.parseDateToStr(strategyDTO.getInsertTime(),"yyyy-MM-dd HH:mm:ss"));
                strategyDTO.setUpdateTimeStr(DateUtils.parseDateToStr(strategyDTO.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
                strategyDTO.setCustomerGroupName(ENUM_CUSTOMER_GROUP_TYPE.getLabelByValue(strategyConfig.getCustomerGroup()));
                strategyDTO.setStrategyChannelName(ENUM_CHANNEL_TYPE.getLabelByValue(strategyConfig.getStrategyChannelCode()));
                strategyDTO.setStrategyStatusName(ENUM_STRATEGY_STATUS_TYPE.getLabelByValue(strategyConfig.getStrategyStatus()));
                strategyDTO.setFlowConfigText("");
                list.add(strategyDTO);
            }
        }
        queryStrategyListResDTO.setRows(list);
        return queryStrategyListResDTO;
    }

    /**
     * 策略配置实例列表查询
     */
    @PostMapping("/queryStrategyInstanceList")
    public QueryStrategyInstanceListResDTO queryStrategyInstanceList(@RequestBody QueryStrategyInstanceListReqDTO queryStrategyInstanceListReqDTO){
        QueryStrategyInstanceListResDTO queryStrategyInstanceListResDTO = new QueryStrategyInstanceListResDTO();
        startPage();
        List<StrategyInstanceDTO> list = new ArrayList<>();
        StrategyInstance strategyInstance = new StrategyInstance();
        BeanUtils.copyBeanProp(strategyInstance,queryStrategyInstanceListReqDTO);
        List<StrategyInstance> strategyInstances = iStrategyInstanceService.findList(strategyInstance);
        TableDataInfo dataTable = getDataTable(strategyInstances);
        queryStrategyInstanceListResDTO.setRows(dataTable.getRows());
        queryStrategyInstanceListResDTO.setTotal(dataTable.getTotal());
        queryStrategyInstanceListResDTO.setMsg(dataTable.getMsg());
        queryStrategyInstanceListResDTO.setCode(dataTable.getCode());
        if(CollectionUtil.isNotEmpty(queryStrategyInstanceListResDTO.getRows())){
            List<StrategyInstance> strategyInstanceList = (List<StrategyInstance>)queryStrategyInstanceListResDTO.getRows();
            for (StrategyInstance strategyInstance1 : strategyInstanceList) {
                StrategyInstanceDTO strategyInstanceDTO = new StrategyInstanceDTO();
                BeanUtils.copyBeanProp(strategyInstanceDTO,strategyInstance1);
                strategyInstanceDTO.setStrategyInstanceId(strategyInstance1.getStrategyInstanceId()+"");
                strategyInstanceDTO.setInsertTimeStr(DateUtils.parseDateToStr(strategyInstance1.getInsertTime(),"yyyy-MM-dd HH:mm:ss"));
                strategyInstanceDTO.setUpdateTimeStr(DateUtils.parseDateToStr(strategyInstance1.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
                strategyInstanceDTO.setCustomerGroupName(ENUM_CUSTOMER_GROUP_TYPE.getLabelByValue(strategyInstance1.getCustomerGroup()));
                strategyInstanceDTO.setStrategyChannelName(ENUM_CHANNEL_TYPE.getLabelByValue(strategyInstance1.getStrategyChannelCode()));
                strategyInstanceDTO.setStrategyInstanceStatusName(ENUM_STRATEGY_INSTANCE_STATUS_TYPE.getLabelByValue(strategyInstance1.getStrategyInstanceStatus()));
                list.add(strategyInstanceDTO);
            }
        }
        queryStrategyInstanceListResDTO.setRows(list);
        return queryStrategyInstanceListResDTO;
    }

    /**
     * 策略配置枚举查询
     */
    @PostMapping("/queryEnumList")
    public QueryEnumListResDTO queryEnumList(@RequestBody QueryEnumListReqDTO queryEnumListReqDTO){
        QueryEnumListResDTO queryStrategyListResDTO = new QueryEnumListResDTO();
        Map<String,Object> map = new HashMap<>();
        map.put("channelType",ENUM_CHANNEL_TYPE.getEnumList());
        map.put("customerGroup", ENUM_CUSTOMER_GROUP_TYPE.getEnumList());
        map.put("activityType", ENUM_ACTIVITY_TYPE.getEnumList());
        map.put("strategyActionType", ENUM_STRATEGY_ACTION_TYPE.getEnumList());
        map.put("strategyDoType", ENUM_STRATEGY_DO_TYPE.getEnumList());
        map.put("strategyExecuteType", ENUM_STRATEGY_EXECUTE_TYPE.getEnumList());
        map.put("strategyNodeType", ENUM_STRATEGY_NODE_TYPE.getEnumList());
        map.put("strategyStatus", ENUM_STRATEGY_STATUS_TYPE.getEnumList());
        map.put("prizeLevel", ENUM_PRIZE_LEVEL.getEnumList());
        List<OrganInfoDTO> organInfoDTOS = JSONArray.parseArray(EnumOverallInfo.organList, OrganInfoDTO.class);
        map.put("organList", JSONArray.toJSONString(organInfoDTOS));
        queryStrategyListResDTO.setEnumList(map);
        return queryStrategyListResDTO;
    }

    /**
     * 策略配置详情查询
     */
    @PostMapping("/queryStrategyById")
    public QueryStrategyByIdResDTO queryStrategyList(@RequestBody QueryStrategyByIdReqDTO queryStrategyByIdReqDTO){
        QueryStrategyByIdResDTO queryStrategyByIdResDTO = new QueryStrategyByIdResDTO();
        long strategyId = Long.parseLong(queryStrategyByIdReqDTO.getStrategyId());
        TStrategyConfig tStrategyConfig = iStrategyService.findById(strategyId);
        if(tStrategyConfig!=null){
            BeanUtils.copyBeanProp(queryStrategyByIdResDTO,tStrategyConfig);
            queryStrategyByIdResDTO.setStrategyId(tStrategyConfig.getStrategyId()+"");
            queryStrategyByIdResDTO.setExecuteStartDateStr(DateUtils.parseDateToStr(tStrategyConfig.getExecuteStartDate(),"yyyy-MM-dd HH:mm:ss"));
            queryStrategyByIdResDTO.setExecuteEndDateStr(DateUtils.parseDateToStr(tStrategyConfig.getExecuteEndDate(),"yyyy-MM-dd HH:mm:ss"));
            queryStrategyByIdResDTO.setInsertTimeStr(DateUtils.parseDateToStr(tStrategyConfig.getInsertTime(),"yyyy-MM-dd HH:mm:ss"));
            queryStrategyByIdResDTO.setUpdateTimeStr(DateUtils.parseDateToStr(tStrategyConfig.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
        }
        return queryStrategyByIdResDTO;
    }

    /**
     * 策略配置删除
     */
    @PostMapping("/deleteStrategyById")
    public DeleteStrategyByIdResDTO deleteStrategyById(@RequestBody DeleteStrategyByIdReqDTO deleteStrategyByIdReqDTO){
        DeleteStrategyByIdResDTO deleteStrategyByIdResDTO = new DeleteStrategyByIdResDTO();
        deleteStrategyByIdResDTO.setCode("200");
        deleteStrategyByIdResDTO.setMessage("删除成功");
        long strategyId = Long.parseLong(deleteStrategyByIdReqDTO.getStrategyId());
        TStrategyConfig tStrategyConfig = iStrategyService.findById(strategyId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tStrategyConfig.setUpdateUser(loginUser.getUser().getUserName()+"");
        tStrategyConfig.setUpdateTime(new Date());
        tStrategyConfig.setIsDelete("1");
        iStrategyService.update(tStrategyConfig);
        return deleteStrategyByIdResDTO;
    }
    /**
     * 策略配置实例结束
     */
    @PostMapping("/deleteStrategyInstanceById")
    public DeleteStrategyInstanceByIdResDTO deleteStrategyInstanceById(@RequestBody DeleteStrategyInstanceByIdReqDTO deleteStrategyInstanceByIdReqDTO){
        DeleteStrategyInstanceByIdResDTO deleteStrategyInstanceByIdResDTO = new DeleteStrategyInstanceByIdResDTO();
        deleteStrategyInstanceByIdResDTO.setCode("200");
        deleteStrategyInstanceByIdResDTO.setMessage("删除成功");
        long strategyInstanceId = Long.parseLong(deleteStrategyInstanceByIdReqDTO.getStrategyInstanceId());
        StrategyInstance strategyInstance = iStrategyInstanceService.findById(strategyInstanceId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        strategyInstance.setUpdateUser(loginUser.getUser().getUserName()+"");
        strategyInstance.setUpdateTime(new Date());
        strategyInstance.setStrategyInstanceStatus(ENUM_STRATEGY_INSTANCE_STATUS_TYPE.YWC.getValue());
        iStrategyInstanceService.update(strategyInstance);
        return deleteStrategyInstanceByIdResDTO;
    }
    /**
     * 策略配置实例结束
     */
    @PostMapping("/checkStrategyById")
    public CheckStrategyByIdResDTO checkStrategyById(@RequestBody CheckStrategyByIdReqDTO checkStrategyByIdReqDTO){
        CheckStrategyByIdResDTO checkStrategyByIdResDTO = new CheckStrategyByIdResDTO();
        checkStrategyByIdResDTO.setCode("200");
        checkStrategyByIdResDTO.setMessage("校验成功");
        long strategyId = Long.parseLong(checkStrategyByIdReqDTO.getStrategyId());
        StrategyInstance strategyInstance = new StrategyInstance();
        strategyInstance.setStrategyId(strategyId);
        strategyInstance.setStrategyInstanceStatus("zxz");
        List<StrategyInstance> list = iStrategyInstanceService.findList(strategyInstance);
        if(CollectionUtil.isNotEmpty(list)){
            checkStrategyByIdResDTO.setCode("500");
            checkStrategyByIdResDTO.setMessage("校验失败");
        }
        return checkStrategyByIdResDTO;
    }

    /**
     * 执行策略
     */
    @PostMapping("/executeStrategyById")
    public ExecuteStrategyByIdResDTO executeStrategyById(@RequestBody ExecuteStrategyByIdReqDTO executeStrategyByIdReqDTO){
        ExecuteStrategyByIdResDTO executeStrategyByIdResDTO = new ExecuteStrategyByIdResDTO();
        executeStrategyByIdResDTO.setCode("200");
        executeStrategyByIdResDTO.setMessage("执行成功");
        strategyInfoService.executeStrategyById(Long.parseLong(executeStrategyByIdReqDTO.getStrategyId()));
        return executeStrategyByIdResDTO;
    }



    /**
     * 复制策略
     */
    @PostMapping("/copyStrategy")
    public CopyStrategyResDTO copyStrategy(@RequestBody CopyStrategyReqDTO copyStrategyReqDTO){
        CopyStrategyResDTO copyStrategyResDTO = new CopyStrategyResDTO();
        copyStrategyResDTO.setCode("200");
        copyStrategyResDTO.setMessage("执行成功");
        TStrategyConfig tStrategyConfig = iStrategyService.findById(Long.parseLong(copyStrategyReqDTO.getStrategyId()));
        iStrategyService.insert(tStrategyConfig);
        return copyStrategyResDTO;
    }

    /**
     * 策略实例详情查询
     */
    @PostMapping("/queryStrategyInstanceById")
    public QueryStrategyInstanceByIdResDTO queryStrategyInstanceById(@RequestBody QueryStrategyInstanceByIdReqDTO queryStrategyInstanceByIdReqDTO){
        QueryStrategyInstanceByIdResDTO queryStrategyInstanceByIdResDTO = new QueryStrategyInstanceByIdResDTO();
        long strategyInstanceId = Long.parseLong(queryStrategyInstanceByIdReqDTO.getStrategyInstanceId());
        //查询实例详情
        StrategyInstance strategyInstance = iStrategyInstanceService.findById(strategyInstanceId);
        StrategyInstanceDTO strategyInstanceDTO = new StrategyInstanceDTO();
        if(strategyInstance!=null){
            BeanUtils.copyBeanProp(strategyInstanceDTO,strategyInstance);
            strategyInstanceDTO.setStrategyInstanceId(strategyInstance.getStrategyInstanceId()+"");
            strategyInstanceDTO.setInsertTimeStr(DateUtils.parseDateToStr(strategyInstance.getInsertTime(),"yyyy-MM-dd HH:mm:ss"));
            strategyInstanceDTO.setUpdateTimeStr(DateUtils.parseDateToStr(strategyInstance.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
            strategyInstanceDTO.setCustomerGroupName(ENUM_CUSTOMER_GROUP_TYPE.getLabelByValue(strategyInstance.getCustomerGroup()));
            strategyInstanceDTO.setStrategyChannelName(ENUM_CHANNEL_TYPE.getLabelByValue(strategyInstance.getStrategyChannelCode()));
            strategyInstanceDTO.setStrategyInstanceStatusName(ENUM_STRATEGY_INSTANCE_STATUS_TYPE.getLabelByValue(strategyInstance.getStrategyInstanceStatus()));
            StrategyInstanceNode strategyInstanceNode = new StrategyInstanceNode();
            strategyInstanceNode.setStrategyInstanceId(strategyInstance.getStrategyInstanceId());
            StrategyBuryingPoint strategyBuryingPoint = new StrategyBuryingPoint();
            strategyBuryingPoint.setStrategyInstanceId(strategyInstance.getStrategyInstanceId());
            //查询埋点节点集合
            List<StrategyBuryingPoint> list1 = iStrategyBuryingPointService.findList(strategyBuryingPoint);
            List<StrategyBuryingPointDTO> strategyBuryingPointDTOS = new ArrayList<>();
            if(CollectionUtil.isNotEmpty(list1)){
                for (StrategyBuryingPoint buryingPoint : list1) {
                    StrategyBuryingPointDTO strategyBuryingPointDTO = new StrategyBuryingPointDTO();
                    BeanUtils.copyBeanProp(strategyBuryingPointDTO,buryingPoint);
                    strategyBuryingPointDTO.setBuryingPointId(buryingPoint.getBuryingPointId()+"");
                    strategyBuryingPointDTO.setStrategyInstanceId(buryingPoint.getStrategyInstanceId()+"");
                    strategyBuryingPointDTO.setNodeId(buryingPoint.getNodeId()+"");
                    strategyBuryingPointDTO.setInsertTimeStr(DateUtils.parseDateToStr(buryingPoint.getInsertTime(),"yyyy-MM-dd HH:mm:ss"));
                    strategyBuryingPointDTO.setUpdateTimeStr(DateUtils.parseDateToStr(buryingPoint.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
                    strategyBuryingPointDTOS.add(strategyBuryingPointDTO);
                }
            }
            //查询目标节点和其父内容节点的统计信息
            List<StrategyTargetInfoDTO> strategyTargetInfoDTOS = strategyInfoService.queryStrategyTargetInfoDTO(strategyInstance.getStrategyInstanceId());
            queryStrategyInstanceByIdResDTO.setStrategyTargetInfoDTOS(strategyTargetInfoDTOS);
            queryStrategyInstanceByIdResDTO.setStrategyBuryingPointDTOList(strategyBuryingPointDTOS);
        }
        queryStrategyInstanceByIdResDTO.setStrategyInstanceDTO(strategyInstanceDTO);
        return queryStrategyInstanceByIdResDTO;
    }

    /**
     * 新增或更新策略配置
     */
    @PostMapping("/insertOrUpdateStrategyList")
    public InsertOrUpdateStrategyListResDTO insertOrUpdateStrategyList(@RequestBody InsertOrUpdateStrategyListReqDTO insertOrUpdateStrategyListReqDTO){
        InsertOrUpdateStrategyListResDTO insertOrUpdateStrategyListResDTO = new InsertOrUpdateStrategyListResDTO();
        String strategyId = insertOrUpdateStrategyListReqDTO.getStrategyId();
        TStrategyConfig tStrategyConfig = new TStrategyConfig();
        BeanUtils.copyBeanProp(tStrategyConfig,insertOrUpdateStrategyListReqDTO);
        String executeStartDateStr = insertOrUpdateStrategyListReqDTO.getExecuteStartDateStr();
        String executeEndDateStr = insertOrUpdateStrategyListReqDTO.getExecuteEndDateStr();
        if(StringUtils.isNotEmpty(executeStartDateStr)){
            Date date = DateUtils.parseStrDate(executeStartDateStr, "yyyy-MM-dd");
            tStrategyConfig.setExecuteStartDate(date);
        }
        if(StringUtils.isNotEmpty(executeEndDateStr)){
            Date date = DateUtils.parseStrDate(executeEndDateStr, "yyyy-MM-dd");
            tStrategyConfig.setExecuteEndDate(date);
        }
        if(StringUtils.isNotEmpty(insertOrUpdateStrategyListReqDTO.getStrategyId())){
            tStrategyConfig.setStrategyId(Long.parseLong(insertOrUpdateStrategyListReqDTO.getStrategyId()));
        }
        if(StringUtils.isNotEmpty(strategyId)){
            //更新
            iStrategyService.update(tStrategyConfig);
            insertOrUpdateStrategyListResDTO.setMsg("更新成功");
        }else{
            //新增
            iStrategyService.insert(tStrategyConfig);
            insertOrUpdateStrategyListResDTO.setMsg("新增成功");
        }
        return insertOrUpdateStrategyListResDTO;
    }

}
